package com.zhuchaooasystem.presenter.home.serviceimage;

import android.app.Activity;

import com.google.gson.Gson;
import com.zhuchaooasystem.api.HttpApi;
import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpFile;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.ServiceImageDeleteEntity;
import com.zhuchaooasystem.entity.ServiceImageEntity;
import com.zhuchaooasystem.entity.UploadServiceImageEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.CameraPickDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class ServiceImagePresenter extends BasePresenter<ServiceImageContract.View> implements ServiceImageContract.Presenter {

    public ServiceImagePresenter(Activity activity, ServiceImageContract.View view) {
        super(activity, view);
    }

    public ArrayList<ServiceImageEntity.ResultBean.SuccessfulBean.PathContentBean> getServiceImageList(List<ServiceImageEntity.ResultBean.SuccessfulBean> serviceImageList) {
        if (null != serviceImageList && serviceImageList.size() > 0) {
            ArrayList<ServiceImageEntity.ResultBean.SuccessfulBean.PathContentBean> imageList = new ArrayList<>();
            for (int i = 0; i < serviceImageList.size(); i++) {
                List<ServiceImageEntity.ResultBean.SuccessfulBean.PathContentBean> pathList = serviceImageList.get(i).getPathContent();
                for (int j = 0; j < pathList.size(); j++) {
                    imageList.add(pathList.get(j));
                }
            }
            return imageList;
        }
        return null;
    }

    public ArrayList<String> getArrayImageList(List<ServiceImageEntity.ResultBean.SuccessfulBean> serviceImageList) {

        if (null != serviceImageList && serviceImageList.size() > 0) {
            ArrayList<String> imageList = new ArrayList<>();
            for (int i = 0; i < serviceImageList.size(); i++) {
                List<ServiceImageEntity.ResultBean.SuccessfulBean.PathContentBean> pathList = serviceImageList.get(i).getPathContent();
                for (int j = 0; j < pathList.size(); j++) {
                    imageList.add(pathList.get(j).getFilePath());
                }
            }
            return imageList;
        }
        return null;
    }

    @Override
    public void getImage() {
        //----
        final int id = mView.getProjectId();
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ID", id + "");
        //----
        Subscription subscription = HttpFactory.getHttpApi().getConstructMaterials(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            ServiceImageEntity entity = (ServiceImageEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.refreshData(entity.getResult().getSuccessful());
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                });
        addSubscrebe(subscription);

    }

    @Override
    public void showPickDialog() {
        CameraPickDialog dialog = new CameraPickDialog(mActivity);
        dialog.show();
        dialog.setOnViewClickListener(new CameraPickDialog.onViewClickListenner() {
            @Override
            public void onCamera(CameraPickDialog dialog) {
                dialog.dismiss();
                mView.onCameraClick();
            }

            @Override
            public void onAlbum(CameraPickDialog dialog) {
                dialog.dismiss();
                mView.onAlbumClick();
            }

        });
    }

    @Override
    public void uploadImage(List<String> imageList) {
        showLoading();
        //----
        final int userId = UserInfo.getInstance().getMemberId();
        String orderCode = mView.getOrderCode();
        int ProjectId = mView.getProjectId();

        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ProjectId", ProjectId + "");
        map.put("OrderCode", orderCode);
        map.put("CusrmerMenber", userId + "");

        HttpFile.upload(mActivity, HttpApi.submitConstructMaterisal, map, imageList, true, new HttpFile.HttpCallBack() {

            @Override
            public void onSusscess(String data) {
                UploadServiceImageEntity entity = new Gson().fromJson(data, UploadServiceImageEntity.class);
                if (null != entity && null != entity.getResult().getSuccessful()) {
                    mView.submitSuccuss();
                } else {
                    ToastUtil.showShort(mActivity, entity.getResult().getError());
                }
                LoadingDiaogDismiss();
            }

            @Override
            public void onError(String meg) {
                super.onError(meg);
                LoadingDiaogDismiss();
            }
        });
    }

    @Override
    public void deleteImage() {
        //----
        final String serviceid = mView.getServiceId();
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("UidArrry", serviceid);
        //----
        Subscription subscription = HttpFactory.getHttpApi().deleteService_image(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            ServiceImageDeleteEntity entity = (ServiceImageDeleteEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.deleteSuccuss(entity.getResult());
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }


}
